package com.clkj.mobileinspector.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;

import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.BDLocation;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.CoordType;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;
import com.bumptech.glide.Glide;
import com.clkj.mobileinspector.R;
import com.clkj.mobileinspector.model.Constant;
import com.clkj.mobileinspector.model.ObserverOnNext;
import com.clkj.mobileinspector.model.OkHttpUtils;
import com.clkj.mobileinspector.model.bean.OperationBean;
import com.clkj.mobileinspector.model.bean.OperationSuccessBean;
import com.clkj.mobileinspector.model.bean.PolicedeptBean;
import com.clkj.mobileinspector.model.bean.ProblemtypeBean;
import com.clkj.mobileinspector.model.bean.QueryBean;
import com.clkj.mobileinspector.model.bean.QuerySuccessBean;
import com.clkj.mobileinspector.model.bean.ResourceApi;
import com.clkj.mobileinspector.ui.dialog.SignatureDialog;
import com.clkj.mobileinspector.utils.JsonUtils;
import com.clkj.mobileinspector.utils.LogUtils;
import com.clkj.mobileinspector.utils.ResourceApiUtils;
import com.google.gson.reflect.TypeToken;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.lws.basic.base.BaseActivity;
import com.lws.basic.utils.common.GsonUtils;
import com.lws.basic.utils.common.MyLogger;
import com.lws.basic.utils.common.MyToast;
import com.lws.basic.utils.common.RxUtils;
import com.lws.basic.utils.common.TimeUtils;
import com.lws.basic.utils.encryption.Base64Utils;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;

//现场督察
public class InspectorRecordActivity extends BaseActivity {
    @BindView(R.id.map)
    MapView mMap;
    @BindView(R.id.iv_evidence1)
    ImageView mIvEvidence1;
    @BindView(R.id.iv_evidence2)
    ImageView mIvEvidence2;
    @BindView(R.id.iv_evidence3)
    ImageView mIvEvidence3;
    @BindView(R.id.iv_evidence4)
    ImageView mIvEvidence4;
    @BindView(R.id.iv_signature)
    ImageView mIvSignature;
    @BindView(R.id.sp_type)
    Spinner mSpType;
    @BindView(R.id.sp_unit)
    Spinner mSpUnit;
    @BindView(R.id.sp_local)
    Spinner mSpLocal;
    @BindView(R.id.et_leader)
    EditText mEtLeader;
    @BindView(R.id.et_policeman)
    EditText mEtPoliceman;
    @BindView(R.id.et_question)
    EditText mEtQuestion;
    @BindView(R.id.btn)
    Button mBtn;
    private BaiduMap mBaiduMap;
    LocationClient mLocationClient;
    private LatLng mPoint;  //当前经纬度位置
    private String mTypeId;   //类型id
    private String mPolicedepId;//局id
    private String mChild;//所id
    private String mSignature; //签名图片
    private String mPic1; //取证图片
    private String mPic2;
    private String mPic3;
    private String mPic4;

    @Override
    public int setLayoutId() {

        return R.layout.activity_inspector_record;
    }

    @Override
    public void initData() {
        super.initData();

        ButterKnife.bind(this);


        if (!TextUtils.isEmpty(getIntent().getStringExtra("id"))) {
            mBtn.setVisibility(View.INVISIBLE);
            List<QueryBean.ParameterBean.ConditionBean.KeyValueListBean> list = new ArrayList<>();
            QueryBean.ParameterBean.ConditionBean.KeyValueListBean bean1 = new QueryBean.ParameterBean.ConditionBean.KeyValueListBean();
            bean1.setKey("id");
            bean1.setRelationOperator("=");
            bean1.setValue(getIntent().getStringExtra("id"));
            list.add(bean1);
           getDataId(list);

        } else {
            initMap();
            getData(null);
        }

    }

    /**
     * 获取数据
     */
    private void getDataId(List<QueryBean.ParameterBean.ConditionBean.KeyValueListBean> keyValueListBeanList) {
        ResourceApi inspectionRecordDetails = ResourceApiUtils.getResourceApi(MainActivity.resourceApiList, Constant.inspectionRecordDetails);

        showLoading();
        List<QueryBean.ParameterBean.OrderByBean> orderByList = new ArrayList<>();


        QueryBean.ParameterBean.PageBean page = new QueryBean.ParameterBean.PageBean();
        page.setPageNo(1);
        page.setPageSize(20);

        String json = JsonUtils.query(keyValueListBeanList, "id,dutyleader,dutycop,problemdescription,evidencepic1,evidencepic2,evidencepic3,evidencepic4,sign,lnglat,firstdept,seconddept,problemname,createdate,createuser,createidcard"
                , inspectionRecordDetails.getResourceId(), orderByList, page, inspectionRecordDetails.getResourceRegionalismCode());
        LogUtils.logUpload("现场督察", "1", inspectionRecordDetails.getResourceId(), inspectionRecordDetails.getResourceAddress(), json, "", "成功", "","");

        OkHttpUtils.post(inspectionRecordDetails.getResourceAddress(), json, QuerySuccessBean.class).compose(this.bindToLifecycle()).subscribe(new ObserverOnNext<QuerySuccessBean>() {
            @Override
            public void onNext(QuerySuccessBean querySuccessBean) {
                dismissLoading();
                if (querySuccessBean.getCode().equals("200")) {
                    QuerySuccessBean.DataBean.DataListBean bean = querySuccessBean.getData().getDataList().get(0);
                      setSpinner(JsonUtils.queryGetFiled(bean.getFieldValues(), "problemname"),
                              JsonUtils.queryGetFiled(bean.getFieldValues(), "firstdept"),
                              JsonUtils.queryGetFiled(bean.getFieldValues(), "seconddept")
                      );
                      mEtLeader.setText(JsonUtils.queryGetFiled(bean.getFieldValues(), "dutyleader"));
                      mEtPoliceman.setText(JsonUtils.queryGetFiled(bean.getFieldValues(), "dutycop"));
                    mEtQuestion.setText(JsonUtils.queryGetFiled(bean.getFieldValues(), "problemdescription"));

                    String path1 = getFilesDir().getPath() + "/" + TimeUtils.getTodayDateTime() + "pic1.jpg";
                    String path2 = getFilesDir().getPath() + "/" + TimeUtils.getTodayDateTime() + "pic2.jpg";
                    String path3 = getFilesDir().getPath() + "/" + TimeUtils.getTodayDateTime() + "pic3.jpg";
                    String path4 = getFilesDir().getPath() + "/" + TimeUtils.getTodayDateTime() + "pic4.jpg";
                    String sign = getFilesDir().getPath() + "/" + TimeUtils.getTodayDateTime() + "sign.jpg";
                    if (!TextUtils.isEmpty(JsonUtils.queryGetFiled(bean.getFieldValues(), "evidencepic1"))){
                        Base64Utils.base64ToFile(JsonUtils.queryGetFiled(bean.getFieldValues(), "evidencepic1"), path1);
                        Glide.with(InspectorRecordActivity.this).load(path1).into(mIvEvidence1);

                    }
                    if (!TextUtils.isEmpty(JsonUtils.queryGetFiled(bean.getFieldValues(), "evidencepic2"))){
                        Base64Utils.base64ToFile(JsonUtils.queryGetFiled(bean.getFieldValues(), "evidencepic2"), path2);
                        Glide.with(InspectorRecordActivity.this).load(path2).into(mIvEvidence2);
                    }
                    if (!TextUtils.isEmpty(JsonUtils.queryGetFiled(bean.getFieldValues(), "evidencepic3"))){
                        Base64Utils.base64ToFile(JsonUtils.queryGetFiled(bean.getFieldValues(), "evidencepic3"), path3);
                        Glide.with(InspectorRecordActivity.this).load(path3).into(mIvEvidence3);

                    }
                    if (!TextUtils.isEmpty(JsonUtils.queryGetFiled(bean.getFieldValues(), "evidencepic4"))){
                        Base64Utils.base64ToFile(JsonUtils.queryGetFiled(bean.getFieldValues(), "evidencepic4"), path4);
                        Glide.with(InspectorRecordActivity.this).load(path4).into(mIvEvidence4);
                    }
                    if (!TextUtils.isEmpty(JsonUtils.queryGetFiled(bean.getFieldValues(), "sign"))){
                        Base64Utils.base64ToFile(JsonUtils.queryGetFiled(bean.getFieldValues(), "sign"), sign);
                        Glide.with(InspectorRecordActivity.this).load(sign).into(mIvSignature);
                    }
                    String[] strArr = JsonUtils.queryGetFiled(bean.getFieldValues(), "lnglat").split(",");
                    //显示位置
                    location(Double.parseDouble(strArr[0]),Double.parseDouble(strArr[1]));
                    marker(Double.parseDouble(strArr[0]),Double.parseDouble(strArr[1]));
                } else {
                    MyToast.show(querySuccessBean.getMessage());
                }
            }
        });
    }
    /**
     * 显示坐标点
     *
     * @param lng 经度
     * @param lat 纬度
     */
    private void marker(double lng, double lat) {
        BaiduMap baiduMap = mMap.getMap();

        //定义Maker坐标点
        LatLng point = new LatLng(lat, lng);
        //构建Marker图标
        BitmapDescriptor bitmap = BitmapDescriptorFactory
                .fromResource(R.mipmap.icon_marker);
        //构建MarkerOption，用于在地图上添加Marker
        OverlayOptions option = new MarkerOptions()
                .position(point)
                .icon(bitmap);
        //在地图上添加Marker，并显示
        baiduMap.addOverlay(option);
    }

    /**
     * 回显下拉列表
     */
    private void setSpinner(String type ,String firstdept ,String child) {
        try {

            List<String> typeList = new ArrayList<>();
            typeList.add(type);

            //类型
            ArrayAdapter adapterType = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, typeList);
            mSpType.setAdapter(adapterType);

            //局
            List<String> policedeptList = new ArrayList<>();
            policedeptList.add(firstdept);
            ArrayAdapter adapterUnit = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, policedeptList);
            mSpUnit.setAdapter(adapterUnit);

            //所

            List<String> childList = new ArrayList<>();
            childList.add(child);
            ArrayAdapter adapterLocal = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, childList);
            mSpLocal.setAdapter(adapterLocal);



        } catch (Exception e) {
            MyToast.show(e.getMessage());
            MyLogger.i("统计分析下拉列表" + e.getMessage());
        }


    }



    /**
     * 初始化下拉列表
     */
    private void initSpinner(List<QuerySuccessBean.DataBean.DataListBean.FieldValuesBean> list) {
        try {

            //类型下拉列表
            String problemtype = JsonUtils.queryGetFiled(list, "problemtype");
            Type type = new TypeToken<List<ProblemtypeBean>>() {
            }.getType();
            List<ProblemtypeBean> problemtypeBeanList = GsonUtils.getInstance().fromJson(problemtype, type);
            List<String> typeList = new ArrayList<>();
            for (ProblemtypeBean bean : problemtypeBeanList) {
                typeList.add(bean.getName());
            }
            //默认类型id
            mTypeId = problemtypeBeanList.get(0).getId() + "";
            //类型
            ArrayAdapter adapterType = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, typeList);
            mSpType.setAdapter(adapterType);
            mSpType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    MyLogger.i(position + "");
                    mTypeId = problemtypeBeanList.get(position).getId() + "";
                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {

                }
            });

            //局
            String policedept = JsonUtils.queryGetFiled(list, "policedept");
            Type policedeptType = new TypeToken<List<PolicedeptBean>>() {
            }.getType();
            List<PolicedeptBean> policedeptBeanList = GsonUtils.getInstance().fromJson(policedept, policedeptType);
            List<String> policedeptList = new ArrayList<>();
            for (PolicedeptBean bean : policedeptBeanList) {
                policedeptList.add(bean.getName());
            }
            mPolicedepId = policedeptBeanList.get(0).getDeptid() + "";
            //所

            List<PolicedeptBean.ChildBean> childBeanList = policedeptBeanList.get(0).getChild();
            List<String> childList = new ArrayList<>();
            for (PolicedeptBean.ChildBean bean : childBeanList) {
                childList.add(bean.getName());
            }
            mChild = childBeanList.get(0).getDeptid() + "";
            ArrayAdapter adapterLocal = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, childList);
            mSpLocal.setAdapter(adapterLocal);
            mSpLocal.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    mChild = childBeanList.get(position).getDeptid() + "";

                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {

                }
            });


            //局
            ArrayAdapter adapterUnit = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, policedeptList);
            mSpUnit.setAdapter(adapterUnit);
            mSpUnit.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    mPolicedepId = policedeptBeanList.get(position).getDeptid() + "";
                    List<String> childList = new ArrayList<>();
                    for (PolicedeptBean.ChildBean bean : policedeptBeanList.get(position).getChild()) {
                        childList.add(bean.getName());
                    }
                    if (policedeptBeanList.get(position).getChild().size()>0) {
                        mChild = policedeptBeanList.get(position).getChild().get(0).getDeptid() + "";
                    }
                    adapterLocal.clear();
                    adapterLocal.addAll(childList);
                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {

                }
            });
        } catch (Exception e) {
            MyToast.show(e.getMessage());
            MyLogger.i("现场督察下拉列表" + e.getMessage());
        }


    }

    /**
     * 获取数据下拉列表
     */
    private void getData(List<QueryBean.ParameterBean.ConditionBean.KeyValueListBean> keyValueListBeanList) {
        ResourceApi policeDept = ResourceApiUtils.getResourceApi(MainActivity.resourceApiList, Constant.policeDept);

        showLoading();
        List<QueryBean.ParameterBean.OrderByBean> orderByList = new ArrayList<>();


        QueryBean.ParameterBean.PageBean page = new QueryBean.ParameterBean.PageBean();
        page.setPageNo(1);
        page.setPageSize(1000);

        String json = JsonUtils.query(keyValueListBeanList, "policedept,problemtype"
                , policeDept.getResourceId(), orderByList, page, policeDept.getResourceRegionalismCode());

        OkHttpUtils.post(policeDept.getResourceAddress(), json, QuerySuccessBean.class).compose(this.bindToLifecycle()).subscribe(new ObserverOnNext<QuerySuccessBean>() {
            @Override
            public void onNext(QuerySuccessBean querySuccessBean) {
                dismissLoading();

                if (querySuccessBean.getCode().equals("200")) {


                    initSpinner(querySuccessBean.getData().getDataList().get(0).getFieldValues());

                } else {
                    MyToast.show(querySuccessBean.getMessage());
                }
            }
        });
    }
    /**
     * 显示经纬度位置
     * @param lng  经度
     * @param lat 纬度
     */
    private void location(double lng, double lat){
        BaiduMap baiduMap = mMap.getMap();
        //定义Maker坐标点
        LatLng point = new LatLng(lat, lng);
        MapStatus mMapStatus = new MapStatus.Builder()
                .target(point)
                .zoom(15)
                .build();
        MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mMapStatus);
        //改变地图状态
        baiduMap.setMapStatus(mMapStatusUpdate);
    }


    /**
     * 初始化地图
     */
    private void initMap() {
        mBaiduMap = mMap.getMap();
        mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
        mBaiduMap.setMyLocationEnabled(true);
        //定位初始化
        mLocationClient = new LocationClient(this);

//通过LocationClientOption设置LocationClient相关参数
        LocationClientOption option = new LocationClientOption();
        option.setOpenGps(true); // 打开gps
        option.setCoorType("bd09ll"); // 设置坐标类型
        option.setScanSpan(10000);

        //设置locationClientOption
        mLocationClient.setLocOption(option);

        //注册LocationListener监听器
        MyLocationListener myLocationListener = new MyLocationListener();
        mLocationClient.registerLocationListener(myLocationListener);
        //开启地图定位图层
        mLocationClient.start();

    }

    @OnClick({R.id.iv_evidence1, R.id.iv_evidence2, R.id.iv_evidence3, R.id.iv_evidence4, R.id.iv_signature, R.id.btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_evidence1:
                openPhoto(1);
                break;
            case R.id.iv_evidence2:
                openPhoto(2);
                break;
            case R.id.iv_evidence3:
                openPhoto(3);
                break;
            case R.id.iv_evidence4:
                openPhoto(4);
                break;
            case R.id.iv_signature:
                SignatureDialog dialog = new SignatureDialog();
                dialog.show(getSupportFragmentManager(), "");
                dialog.setI(new SignatureDialog.I() {
                    @Override
                    public void success(String path) {
                        if (!TextUtils.isEmpty(path)) {
                            Glide.with(InspectorRecordActivity.this).load(path).into(mIvSignature);
                            if (!TextUtils.isEmpty(path)) {
                                mSignature = Base64Utils.imageToBase64(path);
                            }
                        }
                    }
                });
                break;
            case R.id.btn: //提交
                String leader = mEtLeader.getText().toString();
                String question = mEtQuestion.getText().toString();
                String policeman = mEtPoliceman.getText().toString();
                if (TextUtils.isEmpty(leader)) {
                    MyToast.show("责任领导不能为空");
                } else if (TextUtils.isEmpty(question)) {
                    MyToast.show("问题描述不能为空");
                } else {
                    showLoading();

                    //接口
                    ResourceApi nspectionRecordAdd = ResourceApiUtils.getResourceApi(MainActivity.resourceApiList, Constant.inspectionRecordAdd);

                    List<OperationBean.ParameterBean.OperationsBean.DataBean> dataList = new ArrayList<>();
                    OperationBean.ParameterBean.OperationsBean.DataBean data1 = new OperationBean.ParameterBean.OperationsBean.DataBean();
                    List<OperationBean.ParameterBean.OperationsBean.DataBean.FieldValuesBean> fieldValuesBeanList = new ArrayList<>();
                    OperationBean.ParameterBean.OperationsBean.DataBean.FieldValuesBean fieldValues1 = new OperationBean.ParameterBean.OperationsBean.DataBean.FieldValuesBean();
                    fieldValues1.setField("inspecttype");
                    fieldValues1.setValue(mTypeId);
                    fieldValuesBeanList.add(fieldValues1);
                    OperationBean.ParameterBean.OperationsBean.DataBean.FieldValuesBean fieldValues2 = new OperationBean.ParameterBean.OperationsBean.DataBean.FieldValuesBean();
                    fieldValues2.setField("firstleveldutyunit");
                    fieldValues2.setValue(mPolicedepId);
                    fieldValuesBeanList.add(fieldValues2);
                    OperationBean.ParameterBean.OperationsBean.DataBean.FieldValuesBean fieldValues3 = new OperationBean.ParameterBean.OperationsBean.DataBean.FieldValuesBean();
                    fieldValues3.setField("secondleveldutyunit");
                    fieldValues3.setValue(mChild);
                    fieldValuesBeanList.add(fieldValues3);
                    OperationBean.ParameterBean.OperationsBean.DataBean.FieldValuesBean fieldValues4 = new OperationBean.ParameterBean.OperationsBean.DataBean.FieldValuesBean();
                    fieldValues4.setField("dutyleader");
                    fieldValues4.setValue(leader);
                    fieldValuesBeanList.add(fieldValues4);
                    OperationBean.ParameterBean.OperationsBean.DataBean.FieldValuesBean fieldValues5 = new OperationBean.ParameterBean.OperationsBean.DataBean.FieldValuesBean();
                    fieldValues5.setField("dutycop");
                    fieldValues5.setValue(policeman);
                    fieldValuesBeanList.add(fieldValues5);
                    OperationBean.ParameterBean.OperationsBean.DataBean.FieldValuesBean fieldValues6 = new OperationBean.ParameterBean.OperationsBean.DataBean.FieldValuesBean();
                    fieldValues6.setField("problemdescription");
                    fieldValues6.setValue(question);
                    fieldValuesBeanList.add(fieldValues6);
                    OperationBean.ParameterBean.OperationsBean.DataBean.FieldValuesBean fieldValues7 = new OperationBean.ParameterBean.OperationsBean.DataBean.FieldValuesBean();
                    fieldValues7.setField("evidencepic1");
                    fieldValues7.setValue(mPic1);
                    fieldValuesBeanList.add(fieldValues7);
                    OperationBean.ParameterBean.OperationsBean.DataBean.FieldValuesBean fieldValues8 = new OperationBean.ParameterBean.OperationsBean.DataBean.FieldValuesBean();
                    fieldValues8.setField("evidencepic2");
                    fieldValues8.setValue(mPic2);
                    fieldValuesBeanList.add(fieldValues8);
                    OperationBean.ParameterBean.OperationsBean.DataBean.FieldValuesBean fieldValues9 = new OperationBean.ParameterBean.OperationsBean.DataBean.FieldValuesBean();
                    fieldValues9.setField("evidencepic3");
                    fieldValues9.setValue(mPic3);
                    fieldValuesBeanList.add(fieldValues9);
                    OperationBean.ParameterBean.OperationsBean.DataBean.FieldValuesBean fieldValues10 = new OperationBean.ParameterBean.OperationsBean.DataBean.FieldValuesBean();
                    fieldValues10.setField("evidencepic4");
                    fieldValues10.setValue(mPic4);
                    fieldValuesBeanList.add(fieldValues10);
                    OperationBean.ParameterBean.OperationsBean.DataBean.FieldValuesBean fieldValues11 = new OperationBean.ParameterBean.OperationsBean.DataBean.FieldValuesBean();
                    fieldValues11.setField("sign");
                    fieldValues11.setValue(mSignature);
                    fieldValuesBeanList.add(fieldValues11);
                    OperationBean.ParameterBean.OperationsBean.DataBean.FieldValuesBean fieldValues12 = new OperationBean.ParameterBean.OperationsBean.DataBean.FieldValuesBean();
                    fieldValues12.setField("lnglat");
                    fieldValues12.setValue(mPoint.longitude + "," + mPoint.latitude);
                    MyLogger.i(mPoint.longitude + "," + mPoint.latitude);
                    fieldValuesBeanList.add(fieldValues12);


                    data1.setFieldValues(fieldValuesBeanList);
                    dataList.add(data1);
                    String json = JsonUtils.operation(null, 1, nspectionRecordAdd.getResourceId(), dataList, nspectionRecordAdd.getResourceRegionalismCode());

                    OkHttpUtils.post(nspectionRecordAdd.getResourceAddress(), json, OperationSuccessBean.class).compose(this.bindToLifecycle()).subscribe(new ObserverOnNext<OperationSuccessBean>() {
                        @Override
                        public void onNext(OperationSuccessBean operationSuccessBean) {
                            dismissLoading();
                            if (operationSuccessBean.getCode().equals("200")) {
                                MyToast.show("操作成功");
                                finish();

                            } else {
                                MyToast.show(operationSuccessBean.getMessage());
                            }

                        }


                    });

                }


                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();

        mMap.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();

        mMap.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mLocationClient != null) {
            mLocationClient.stop();
        }
        if (mBaiduMap != null) {
            mBaiduMap.setMyLocationEnabled(false);
        }
        mMap.onDestroy();
        mMap = null;

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    /**
     * 打开相册
     */
    public void openPhoto(int request) {
        PictureSelector.create(this)
                .openGallery(PictureMimeType.ofImage())
                .minSelectNum(1)
                .selectionMode(PictureConfig.SINGLE)
                .previewImage(true)
                .isCamera(true)
                .compress(true)
                .forResult(request);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == RESULT_OK) {
            final List<LocalMedia> list = PictureSelector.obtainMultipleResult(data);
            MyLogger.i(list.get(0).getCompressPath());
            switch (requestCode) {
                case 1:
                    Glide.with(InspectorRecordActivity.this).load(list.get(0).getCompressPath()).into(mIvEvidence1);                    //图片转成base64字符串
                    mPic1 = Base64Utils.imageToBase64(list.get(0).getCompressPath());
                    break;
                case 2:
                    Glide.with(InspectorRecordActivity.this).load(list.get(0).getCompressPath()).into(mIvEvidence2);                    //图片转成base64字符串
                    mPic2 = Base64Utils.imageToBase64(list.get(0).getCompressPath());
                    break;
                case 3:
                    Glide.with(InspectorRecordActivity.this).load(list.get(0).getCompressPath()).into(mIvEvidence3);                    //图片转成base64字符串
                    mPic3 = Base64Utils.imageToBase64(list.get(0).getCompressPath());
                    break;
                case 4:
                    Glide.with(InspectorRecordActivity.this).load(list.get(0).getCompressPath()).into(mIvEvidence4);                    //图片转成base64字符串
                    mPic4 = Base64Utils.imageToBase64(list.get(0).getCompressPath());
                    break;

            }
        }


    }

    public class MyLocationListener extends BDAbstractLocationListener {
        @Override
        public void onReceiveLocation(BDLocation location) {
            if (location== null){
                MyLogger.i("location==null");
            }
            MyLogger.i(location.toString());

            //mapView 销毁后不在处理新接收的位置
            if (location == null || mMap == null) {
                return;
            }
            MyLocationData locData = new MyLocationData.Builder()
                    .accuracy(location.getRadius())
                    // 此处设置开发者获取到的方向信息，顺时针0-360
                    .direction(location.getDirection()).latitude(location.getLatitude())
                    .longitude(location.getLongitude()).build();
            mBaiduMap.setMyLocationData(locData);
            mPoint = new LatLng(location.getLatitude(), location.getLongitude());
            MyLogger.i(location.getLatitude()+"----"+location.getLongitude());
            MapStatus mMapStatus = new MapStatus.Builder()
                    .target(mPoint)
                    .zoom(15)
                    .build();
            MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mMapStatus);
            //改变地图状态
            mBaiduMap.setMapStatus(mMapStatusUpdate);


        }
    }


}
